/*
 * @Descripttion: 
 * @version: 
 * @Author: lwj
 * @Date: 2022-06-14 18:56:19
 * @LastEditors: lwj
 * @LastEditTime: 2022-07-06 10:52:06

/**
 * @name: 手机号码验证
 * @msg: mobile
 * @return {*}  Boolean
 */
const validateMobile = value => {
  const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (!value) {
    wx.showToast({
      title: '请输入手机号',
      icon: 'none'
    })
    return false
  }
  if (!reg.test(value)) {
    wx.showToast({
      title: '请输入正确的手机号',
      icon: 'none'
    })
    return false
  } else {
    return true
  }
}

/**
 * @name: 手机号码+座机号验证
 * @msg: mobile
 * @return {*}  Boolean
 */
const validateMobile2 = value => {
  if (!value) {
    wx.showToast({
      title: '请输入手机号',
      icon: 'none'
    })
    return false
  }
  let regPone = null;
  let mobile = /^1(3|4|5|6|7|8|9)\d{9}$/; //最新16手机正则
  let tel = /^(0\d{2,3}){0,1}\d{7,8}$/; //座机
  // let tel = /^(0\d{2,3}-){0,1}\d{7,8}$/; //座机
  if (value.charAt(0) == 0) { // charAt查找第一个字符方法，用来判断输入的是座机还是手机号
    regPone = tel;
  } else {
    regPone = mobile;
  }
  if (!regPone.test(value)) {
    wx.showToast({
      title: '请输入正确的手机号或座机号',
      icon: 'none'
    })
    return false
  } else {
    return true
  }
}

/**
 * @name: 密码验证
 * @msg: pwd
 * @return {*}  Boolean
 */
const validatePwd = value => {
  // const reg = /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,16}$/;
  const reg =/^(?=.*[a-zA-Z]).{6,16}$/
  if (!value) {
    wx.showToast({
      title: '请输入密码',
      icon: 'none'
    })
    return false
  }
  if (!reg.test(value)) {
    wx.showToast({
      title: '密码必须是6-16位的数字、字母组成(不能是纯数字)',
      icon: 'none'
    })
    return false
  } else {
    return true
  }
}

/**
 * @name: 用户名验证   数字+字母组成
 * @msg: 
 * @return {*}  Boolean
 */
const validateUsername = (value) => {
  const reg1 = /^[0-9]*$/;
  const reg2 = /^[A-Za-z0-9]+$/;
  if (!value) {
    wx.showToast({
      title: '请输入用户名',
      icon: 'none'
    })
    return false
  }
  console.log(value,"value")
  if (reg1.test(value)) {
    wx.showToast({
      title: '用户名不能是纯数字',
      icon: 'none'
    })
    return false
  } else if (!reg2.test(value)) {
    wx.showToast({
      title: '用户名由数字、字母组成',
      icon: 'none'
    })
    return false
  } else if (value.length < 2) {
    wx.showToast({
      title: '用户名长度最少为2位',
      icon: 'none'
    })
    return false
  } else {
    return true
  }
}

/**
 * @name: 表情符号
 * @msg: 
 * @return {*}  Boolean
 */
const validateEmoji = value => {
  return value.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/g, '')
}

/**
 * @name: 特殊符号
 * @msg: 
 * @return {*}  Boolean
 */
const validateSpecial = value => {
  const reg = new RegExp(/[&]/g, '');
  if (reg.test(value)) {
    wx.showToast({
      title: '暂不支持特殊符号"&"',
      icon: 'none'
    })
    return false
  } else {
    return true
  }
}

// 身份证号
const checkRegNo = value => {
  if (value == '') return true
  if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
    wx.showToast({
      title: '你输入的身份证长度或格式错误',
      icon: 'none'
    })
    return false
  }
  //身份证城市
  var aCity = {
    11: "北京",
    12: "天津",
    13: "河北",
    14: "山西",
    15: "内蒙古",
    21: "辽宁",
    22: "吉林",
    23: "黑龙江",
    31: "上海",
    32: "江苏",
    33: "浙江",
    34: "安徽",
    35: "福建",
    36: "江西",
    37: "山东",
    41: "河南",
    42: "湖北",
    43: "湖南",
    44: "广东",
    45: "广西",
    46: "海南",
    50: "重庆",
    51: "四川",
    52: "贵州",
    53: "云南",
    54: "西藏",
    61: "陕西",
    62: "甘肃",
    63: "青海",
    64: "宁夏",
    65: "新疆",
    71: "台湾",
    81: "香港",
    82: "澳门",
    91: "国外"
  };
  if (!aCity[parseInt(value.substr(0, 2))]) {
    wx.showToast({
      title: '你的身份证地区非法',
      icon: 'none'
    })
    return false
  }
  // 出生日期验证
  var sBirthday = (
      value.substr(6, 4) +
      "-" +
      Number(value.substr(10, 2)) +
      "-" +
      Number(value.substr(12, 2))
    ).replace(/-/g, "/"),
    d = new Date(sBirthday);
  if (
    sBirthday !=
    d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
  ) {
    wx.showToast({
      title: '你输入的身份证号非法',
      icon: 'none'
    })
    return false
  }

  // 身份证号码校验
  var sum = 0,
    weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
    codes = "10X98765432";
  for (var i = 0; i < value.length - 1; i++) {
    sum += value[i] * weights[i];
  }
  var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  if (value[value.length - 1] != last) {
    wx.showToast({
      title: '你输入的身份证号非法',
      icon: 'none'
    })
    return false
  }
  return true
}


module.exports = {
  validateMobile,
  validatePwd,
  validateUsername,
  validateEmoji,
  validateSpecial,
  validateMobile2,
  checkRegNo
}